<template>
  <div>
    <el-form :model="form" label-width="auto" inline>
      <el-form-item>
        <el-select v-model="select" placeholder="Select" style="width: 115px">
          <el-option label="采购时间" value="2" />
          <el-option label="提交时间" value="3" />
        </el-select>
        <el-date-picker v-model="form.times" type="datetimerange" start-placeholder="开始时间" value-format="x"
          end-placeholder="结束时间" time-format="A hh:mm:ss" />
      </el-form-item>
      <el-form-item label="采购平台:">
        <el-select v-model="form.purchasePlatformId">
          <el-option label="全部" :value="0"></el-option>
          <el-option :label="item.value" :value="item.key"
            v-for="item in store.productPurchaseProcessCreatePurchaseProcessDicArr" :key="item.key"
            @click="getSon(item)" />
        </el-select>
      </el-form-item>
      <el-form-item label="采购账号:">
        <el-select v-model="form.purchaseCustomerName">
          <el-option :label="item.value" :value="item.name" v-for="item in sonSelect" :key="item.key" />
        </el-select>
      </el-form-item>
      <el-form-item label="采购订单号:">
        <el-input v-model="form.productPurchaseOrderCode" clearable />
      </el-form-item>
      <el-form-item label="上家物流单号:">
        <el-input v-model="form.trackingNumbers" clearable />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">搜索</el-button>
      </el-form-item>
    </el-form>
  </div>
  <div class="table">
    <el-table ref="multipleTableRef" :data="tableData" style="width: 100%" @selection-change="handleSelectionChange"
      border>
      <!-- <el-table-column type="selection" width="55" /> -->
      <el-table-column label="商品图片">
        <template #default="scope">
          <el-image style="width: 40px; height: 40px" :src="scope.row.skuPicture" />
        </template>
      </el-table-column>
      <el-table-column label="采购平台">
        <template #default="scope">{{ scope.row.purchasePlatformName }}</template>
      </el-table-column>
      <el-table-column label="采购账号" width="180">
        <template #default="scope">{{ scope.row.purchaseCustomerName }}</template>
      </el-table-column>
      <el-table-column label="采购时间" width="180">
        <template #default="scope">{{ scope.row.submitTime }}</template>
      </el-table-column>
      <el-table-column label="采购订单号" width="180">
        <template #default="scope">{{ scope.row.productPurchaseOrderCode }}</template>
      </el-table-column>
      <el-table-column label="商品信息" width="180">
        <template #default="scope">{{ scope.row.shopGoodSkuId }}</template>
      </el-table-column>
      <el-table-column label="采购数量">
        <template #default="scope">{{ scope.row.purchaseNum }}</template>
      </el-table-column>
      <el-table-column label="上家订单状态">
        <template #default="scope">{{ scope.row.processOrderStatus == 0 ? '待发货' :
          scope.row.processOrderStatus == 1 ? '已发货' : scope.row.processOrderStatus == 1 ? '待签收' : '' }}</template>
      </el-table-column>
      <el-table-column label="上家物流单号">
        <template #default="scope">{{ scope.row.trackingNumbers }}</template>
      </el-table-column>
      <el-table-column label="质检结果">
        <template #default="scope">{{ scope.row.processQualityStatus == 0 ? '未质检' : scope.row.processQualityStatus == 1
          ? '质检通过' :
          scope.row.processQualityStatus == 2 ? '部分通过' : scope.row.processQualityStatus == 3 ? '不通过' : '' }}</template>
      </el-table-column>
      <el-table-column label="操作" width="180">
        <template #default="scope">
          <el-button link type="primary" size="small" @click.prevent="qualityTestingBtn(scope.row)">
            质检
          </el-button>
          <el-button link type="primary" size="small" @click.prevent="supplementBtn(scope.row)">
            补充上家物流单号
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
  <div class="page">
    <el-pagination v-model:current-page="currentPage4" v-model:page-size="pageSize4" :page-sizes="[10, 50, 100]"
      layout="total, sizes, prev, pager, next, jumper" :total="total" @size-change="handleSizeChange"
      @current-change="handleCurrentChange" />
  </div>
  <!-- 质检 -->
  <el-dialog v-model="qualityTestingDialogVisible" width="500">
    <span>
      <div style="display: flex; flex-direction: column;">
        <label>
          <input type="radio" :value="1" v-model="qualityTestingForm.processQualityStatus" @click="openOrDOng">
          质检通过
        </label>
        <label>
          <input type="radio" :value="2" v-model="qualityTestingForm.processQualityStatus"
            @click="openQualityTestingSon">
          质检部分通过
        </label>
        <label>
          <input type="radio" :value="3" v-model="qualityTestingForm.processQualityStatus"
            @click="openQualityTestingSon">
          质检不通过
        </label>
      </div>
    </span>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="qualityTestingDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="onSubmitQualityTesting">
          保存
        </el-button>
      </div>
    </template>
  </el-dialog>
  <el-dialog v-model="qualityTestingContentDialogVisible" width="500">
    <span>
      <el-form :model="qualityTestingForm" style="max-width: 600px">
        <el-form-item label="未通过数量：">
          <el-input v-model="qualityTestingForm.processQualityRefuseNum" style="width: 120px" />
        </el-form-item>
        <el-form-item label="未通过原因:">
          <el-input v-model="qualityTestingForm.processQualityRefuseReason" type="textarea" />
        </el-form-item>

      </el-form>
    </span>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="qualityTestingContentDialogVisible = false, qualityTestingForm = {}">取消</el-button>
        <el-button type="primary" @click="qualityTestingContentDialogVisible = false">
          保存
        </el-button>
      </div>
    </template>
  </el-dialog>
  <el-dialog v-model="supplementContentDialogVisible" width="500">
    <span>
      <el-form :model="supplementForm" style="max-width: 600px">
        <el-form-item label="采购订单号：">
          <el-input v-model="purchaseProcessId" style="width: 120px" disabled="true" />
        </el-form-item>
        <el-form-item label="上家物流单号:">
          <el-input v-model="supplementForm.trackingNumbers" type="textarea" />
        </el-form-item>

      </el-form>
    </span>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="supplementContentDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="supplementFormSubmit">
          保存
        </el-button>
      </div>
    </template>
  </el-dialog>

  <el-dialog v-model="dialogVisibles" width="600">
    <span>
      <div style="display: flex">
        <label>
          <input type="radio" :value="1" v-model="dialogVisiblesForm">
          自动分配库区库位：
        </label>
        <label>
          <input type="radio" :value="2" v-model="dialogVisiblesForm">
          手动分配库区库位
        </label>
      </div>
      <div v-if="dialogVisiblesForm === 2">
        库区：<el-select v-model="warehouseKqu" filterable>
          <el-option :label="item.areaName" :value="item.areaId" v-for="item in dialogVisiblesHOuse"
            @click="pushWare(item.warehouseLocationAllWithNameVOList)" :key="item.key" />
        </el-select>
        <div ref="NUMDom" v-for="(item, index) in numsKey   " :key='index'>
          库位号：<el-select v-model="warehouseLocationSelectId[index]" filterable>
            <el-option :label="item.name" :value="item.id" v-for="item in warehouseKwSelect" :key="item.key" />
          </el-select>
          数量:<el-input v-model="numsKeyObj[index]" type="number" style="width: 200px;" /><el-icon
            @click="upNum(NUMDom)">
            <Plus />
          </el-icon><el-icon @click="downNum" style="margin-left: 20px;">
            <Minus />
          </el-icon>
        </div>
      </div>
    </span>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogVisibles = false">取消</el-button>
        <el-button type="primary" @click="submintDialog">
          确定
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref, onBeforeMount } from 'vue'
import { productPurchaseProceslist, productPurchaseProcesscreatePurchaseProcessDic, productPurchaseProcessprintProductWithStorageLocation, updatePurchaseProcessTrackingNumber, productPurchaseProcesswarehouseLocationCanUseList } from '@/api/index'
import { useAlertsStore } from '@/store/index'
import { ElMessage } from 'element-plus'
const store = useAlertsStore()
// do not use same name with ref
const form = ref({
  createTimeStart: '',
  createTimeEnd: '',
  submitTimeStart: '',
  submitTimeEnd: '',
  purchasePlatformId: 0,
  purchaseCustomerName: '',
  productPurchaseOrderCode: '',
  trackingNumbers: '',
  pageSize: 10,
  pageNum: 1
})
const tableData = ref([])
onBeforeMount(() => {
  store.productPurchaseProcessCreatePurchaseProcessDicBtn()
})
const onSubmit = () => {
  getList()
  console.log('submit!')
}
const times = ref('')
const select = ref('2')
const getList = () => {
  if (times.value) {
    if (select.value === '2') {
      form.value.createTimeStart = times.value[0]
      form.value.createTimeEnd = times.value[1]
    } else if (select.value === '3') {
      form.value.submitTimeStart = times.value[0]
      form.value.submitTimeEnd = times.value[1]
    }
  } else {
    form.value.createTimeStart = ''
    form.value.createTimeEnd = ''
    form.value.submitTimeStart = ''
    form.value.submitTimeEnd = ''
  }
  productPurchaseProceslist(form.value).then(res => {
    const { data: { data } } = res
    total.value = data.total
    tableData.value = data.list
  })
}
// 分页
const currentPage4 = ref(1)
const pageSize4 = ref(10)
const total = ref(0)
const handleSizeChange = (val) => {
  console.log(`${val} items per page`)
  form.value.pageSize = val
  getList()
}
const handleCurrentChange = (val) => {
  console.log(`current page: ${val}`)
  form.value.pageNum = val
  getList()
}

// table
const multipleSelection = ref([])
const handleSelectionChange = (val) => {
  multipleSelection.value = val
}

// 质检
const qualityTestingDialogVisible = ref(false)
const qualityTestingContentDialogVisible = ref(false)
const qualityTestingBtn = (row) => {
  qualityTestingDialogVisible.value = true
  qualityTestingFormid.value = row.id
}
const qualityTestingFormid = ref('')
const qualityTestingForm = ref({ processQualityRefuseNum: 0 })
const onSubmitQualityTesting = () => {
  productPurchaseProcesscreatePurchaseProcessDic(qualityTestingForm.value, qualityTestingFormid.value).then(res => {
    ElMessage.success('操作成功')
    qualityTestingDialogVisible.value = false
    getList()
  })
}
const sonSelect = ref([])
const getSon = (item) => {
  sonSelect.value = item.purchaseCustomerDics
  console.log(sonSelect.value)
}
const openQualityTestingSon = () => {
  qualityTestingContentDialogVisible.value = true
}

const supplementContentDialogVisible = ref(false)
const purchaseProcessId = ref('')
const supplementForm = ref({})
const supplementFormSubmit = () => {
  // supplementContentDialogVisible.value = true
  updatePurchaseProcessTrackingNumber(supplementForm.value.id, { trackingNumbers: supplementForm.value.trackingNumbers }).then(res => {
    supplementContentDialogVisible.value = false
    ElMessage.success('操作成功')
    getList()
  })
}
const supplementBtn = (row) => {
  supplementContentDialogVisible.value = true
  purchaseProcessId.value = row.productPurchaseOrderCode
  supplementForm.value.id = row.id
}

const dialogVisibles = ref(false)
const NUMDom = ref('')
const numsKey = ref(1)
const warehouseLocationSelectId = ref({})
const numsKeyObj = ref({})
const dialogVisiblesForm = ref(1)
const upNum = (NUMDom) => {
  numsKey.value++
}
const downNum = () => {
  numsKey.value--
}
const warehouseKqu = ref('')
const dialogVisiblesHOuse = ref('')
const openOrDOng = () => {
  productPurchaseProcesswarehouseLocationCanUseList(qualityTestingFormid.value, {
    areaName: ''
  }).then(res => {
    const { data: { data } } = res
    dialogVisiblesHOuse.value = data
  })
  dialogVisibles.value = true
}
const submintDialog = () => {
  if (dialogVisiblesForm.value === 1) {
    productPurchaseProcessprintProductWithStorageLocation({
      type: 0,
      productPurchaseStorageWarehouseLocationListParamList: [
      ]
    }, qualityTestingFormid.value).then(res => {
      ElMessage.success('操作成功')
      dialogVisibles.value = false
      qualityTestingDialogVisible.value = false
    })
  }
  if (dialogVisiblesForm.value === 2) {
    const a = []
    for (let index = 0; index < numsKey.value; index++) {
      console.log('11111')
      console.log(dialogVisiblesForm.value)
      a.push({
        warehouseLocationId: warehouseLocationSelectId.value[index],
        cargoMargin: numsKeyObj.value[index]
      })
      console.log(a)
    }

    productPurchaseProcessprintProductWithStorageLocation({
      type: 1,
      productPurchaseStorageWarehouseLocationListParamList: a
    }, qualityTestingFormid.value).then(res => {
      ElMessage.success('操作成功')
      dialogVisibles.value = false
      qualityTestingDialogVisible.value = false
    })
  }
}
const warehouseKwSelect = ref([])
// const dialogVisiblesForm = ref(1)
const pushWare = (row) => {
  warehouseKwSelect.value = row
}
</script>

<style lang='less' scoped></style>
